<?php

namespace App\Console\Commands;

use App\Models\City;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class GetCity extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'get:city';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client(['verify'=>false]);

        //获取省
        /*$url = 'https://restapi.amap.com/v3/config/district?key=8cd940ff99b662d0a4fe780bedb806bf&subdistrict=1';
        $res = $client->get($url);
        $contents = json_decode($res->getBody()->getContents(),1);

        //print_r($contents['districts'][0]['districts']);exit;

        foreach ($contents['districts'][0]['districts'] as $province) {
            City::create(['name'=>$province['name'],'level'=>$province['level']]);
        }*/

        //获取市
//        $provinces = City::where('level','province')->get()->toArray();
//        //$provinces = City::where('level','province')->where('name','山东省')->get()->toArray();
//        //print_r($provinces);exit;
//        foreach ($provinces as $province) {
//            $url = 'https://restapi.amap.com/v3/config/district?keywords='.$province['name'].'&key=8cd940ff99b662d0a4fe780bedb806bf';
//            print_r($url);echo "\n";
//
//            $res = $client->get($url);
//            $contents = json_decode($res->getBody()->getContents(),1);
//
//            //print_r($contents['districts'][0]['districts']);exit;
//            foreach ($contents['districts'][0]['districts'] as $city) {
//                City::create([
//                    'name'  =>  $city['name'],
//                    'pname' =>  $province['name'],
//                    'level' =>  $city['level']
//                ]);
//            }
//        }

        //获取区、县
        /*$cities = City::where('level','city')->get()->toArray();
        //$provinces = City::where('level','province')->where('name','山东省')->get()->toArray();
        //print_r($cities);exit;
        foreach ($cities as $city) {
            $url = 'https://restapi.amap.com/v3/config/district?keywords='.$city['name'].'&key=8cd940ff99b662d0a4fe780bedb806bf';

            print_r($url);echo "\n";

            $res = $client->get($url);
            $contents = json_decode($res->getBody()->getContents(),1);

            //print_r($contents['districts'][0]['districts']);exit;

            foreach ($contents['districts'][0]['districts'] as $district) {
                City::create([
                    'name'  =>  $district['name'],
                    'pname' =>  $city['name'],
                    'level' =>  $district['level']
                ]);
            }
        }*/

    }
}
